const express = require('express')
//解决跨域问题
const cors = require('cors')
const pool = require('./pool')
const proxy = require('express-http-proxy')
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express()
app.use(cors({
    origin: '*'
}))
app.use((req, res, next) => {
    res.setHeader('Access-Control-Allow-Origin', '*')
    next();
})
app.use('/qy', proxy('https://www.qyer.com/'))
app.use('/pl', proxy('https://place.qyer.com/qcross/place', {
    userResHeaderDecorator(headers, userReq, userRes, proxyReq, proxyRes) {
        // recieves an Object of headers, returns an Object of headers.
        headers['access-control-allow-origin'] = '*'
        return headers;
    }
}))
app.use('/gd', proxy('https://guide.qyer.com'))

// 使用路由
const indexRouter = require('./router/index')
const placeRouter = require('./router/place')
const xczsRouter = require('./router/xczs')
const jdRouter = require('./router/jd')
const sqRouter = require('./router/sq')
const scRouter = require('./router/sc')
const loginRouter = require('./router/login')
const registerRouter = require('./router/register')
app.listen(3000, () => {
    console.log('服务器启动成功')
})
//解决跨域
app.use(express.urlencoded({
    extended: true
}))
//使用路由器
app.use('/v1/index', indexRouter)
app.use('/v1/place', placeRouter)
app.use('/v1/xczs', xczsRouter)
app.use('/v1/jd', jdRouter)
app.use('/v1/sq', sqRouter)
app.use('/v1/sc', scRouter)
app.use('/v1/login', loginRouter)
app.use('/v1/register', registerRouter)
app.use(express.static('public'))
//报错问题

// 解决post传参
app.use((err, req, res, next) => {
    console.log(err)
    res.status(500).send({ code: 500, msg: '服务器端错误' })
})